package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Program.Manager;
import com.groupseven.backend.domain.Program.Program;
import com.groupseven.backend.domain.Program.ProgramApplication;
import com.groupseven.backend.domain.Program.TeamMember;
import com.groupseven.backend.mapper.ProgramMapper;
import com.groupseven.backend.services.ProgramService;
import com.groupseven.backend.test.DataEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class ProgramServiceImpl implements ProgramService {
    @Autowired
    private ProgramMapper programMapper;
    @Autowired
    private DataEncode dataEncode;

    /**
     * @Author 王志华
     * @param pro Program类型
     * @return
     */
    public List<Program> selectByManager(Program pro){
        return programMapper.selectByManager(pro.getManager());
    }

    /**
     * @Author 王志华
     * @param program Program类型
     * @return
     */
    public int modifyByProgramNumber(Program program){
        if (programMapper.selectByProgramNumber(program)==null) {
            return 0;
        }
        return programMapper.modifyByProgramNumber(program);
    }

    /**
     * @Author 王志华
     * @param program Program类型
     * @return
     */
    public int isProgramExits(Program program) {
        program.setProgramNumber(dataEncode.encodeBae64(program.getProgramNumber()));
        if (programMapper.selectByProgramNumber(program)==null){
            return 0;
        }
        return 1;
    }

    /**
     * @Author 王志华
     * @param program Program类型
     * @return
     */
    public int insertProgramInfo(Program program){
//        program.setProgramname(dataEncode.encodeBae64(program.getProgramname()));
//        program.setProgramNumber(dataEncode.encodeBae64(program.getProgramNumber()));
//        program.setLevel(dataEncode.encodeBae64(program.getLevel()));
//        program.setPhone(dataEncode.encodeBae64(program.getPhone()));
//        program.setAchievementUnit(dataEncode.encodeBae64(program.getAchievementUnit()));
//        program.setAffiliation(dataEncode.encodeBae64(program.getAffiliation()));
//        program.setApprovalNumber(dataEncode.encodeBae64(program.getApprovalNumber()));
//        program.setApprovalStatus(dataEncode.encodeBae64(program.getApprovalStatus()));
//        program.setCategory(dataEncode.encodeBae64(program.getCategory()));
//        program.setCurrentUnit(dataEncode.encodeBae64(program.getCurrentUnit()));
//        program.setEmployeeNumber(dataEncode.encodeBae64(program.getEmployeeNumber()));
//        program.setEndDate(dataEncode.encodeBae64(program.getEndDate()));
//        program.setFirs_level_discipline(dataEncode.encodeBae64(program.getFirs_level_discipline()));
//        program.setIsMatched(dataEncode.encodeBae64(program.getIsMatched()));
//        program.setItemLevel(dataEncode.encodeBae64(program.getItemLevel()));
//        program.setPlanCompleted(dataEncode.encodeBae64(program.getPlanCompleted()));
//        program.setProject_sub_category(dataEncode.encodeBae64(program.getProject_sub_category()));
//        program.setProjectDate(dataEncode.encodeBae64(program.getProjectDate()));
//        program.setProjectStatus(dataEncode.encodeBae64(program.getProjectStatus()));
//        program.setProjectType(dataEncode.encodeBae64(program.getProjectType()));
//
        if(programMapper.selectByProgramNumber(program)!=null){
            return 0;
        }
            return programMapper.insertProgramInfo(program);
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<Program> selectAllPrograms(){
        return programMapper.selectAllPrograms();
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<ProgramApplication> findAllProgamApplicationInfo(){
        return programMapper.findAllProgamApplicationInfo();
    }

    /**
     * @Author 王志华
     * @param applications
     * @return
     */
    public List<ProgramApplication> setManagerAndTeamMembers(List<ProgramApplication> applications){
        for (ProgramApplication i : applications){
            Manager manager = programMapper.findManagerByProgramNumber(i.getProgramNumber());
            List<TeamMember> members = programMapper.findTeamMemberByProgramNumber(i.getProgramNumber());
            i.setManager(manager);
            i.setTeamMembers(members);
        }
        return applications;
    }

    /**
     * @Author 王志华
     * @param application
     * @return
     */
    public int insertProgramApplication(ProgramApplication application){
        return programMapper.addProgramApplications(application);
    }

    /**
     * @Author 王志华
     * @param manager
     * @return
     */
    public int insertManagerForApplication(Manager manager){
        return programMapper.addManagerForApplication(manager);
    }

    /**
     * @Author 王志华
     * @param member
     * @return
     */
    public int insertTeamMemberForApplication(TeamMember member){
        return programMapper.addTeamMemberForApplication(member);
    }

    /**
     * @Author 王志华
     * @return
     */
    public String findMaxProgramNumberFromProgramApplication(){
        return programMapper.findMaxProgramNumberFromProgramApplication();
    }

    /**
     * @Author 王志华
     * @param str
     * @return
     */
    public int deleteByProgramNumbers(List<String> str){
        int x = 0;
        for (String i : str){
            if(programMapper.deleteByProgramNumber(i)==1){
                x++;
            }
            if(programMapper.deleteProgramApplicationByProgramNumber(i)==1){
                x++;
            }
        }
        return x;
    }

    /**
     * @Author 王志华
     * @param str
     * @return
     */
    public int deleteByProgramNumber(String str){
        int x =  programMapper.deleteProgramApplicationByProgramNumber(str);
        int y = programMapper.deleteByProgramNumber(str);
        int result = x + y;
        return result;

    }

    /**
     * @Author 王志华
     * @param query
     * @return
     */
    public int multipleDelete(Map<String,Object> query){
        return programMapper.multipleDelete(query);
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<ProgramApplication> findUnreviewed(){
        return programMapper.findUnreviewed();
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<ProgramApplication> findReviewed(){
        return programMapper.findReviewed();
    }

    public List<Program> FuzzySearch(String query){
        query = "%"+query+"%";
        List<Program> result = new ArrayList<>();
        result.addAll(programMapper.FuzzySearch(query));
        return result;
    }

    public ProgramApplication findProgramNumberFromProgramApplication(String ProgramNumber){
        return programMapper.findProgramNumberFromProgramApplication(ProgramNumber);
    }
    ///
    public int updateProgramApplicationByProgramNumber(String ProgramNumber){
        if (programMapper.findProgramNumberFromProgramApplication(ProgramNumber)==null){
            return 0;
        }
        else return programMapper.updateProgramApplicationByProgramNumber(ProgramNumber);
    }

    public int updateManagerForApplication(String ProgramNumber){
        return programMapper.updateManagerForApplication(ProgramNumber);
    }

    public int updateTeamMemberForApplication(String ProgramNumber){
        return programMapper.updateTeamMemberForApplication(ProgramNumber);
    }

    public int deleteProgramApplicationByProgramNumber(String ProgramNumber){
        return programMapper.deleteProgramApplicationByProgramNumber(ProgramNumber);
    }

    public int deleteManagerForApplication(String ProgramNumber){
        return programMapper.deleteManagerForApplication(ProgramNumber);
    }

    public int deleteTeamMemberForApplication(String ProgramNumber){
        return programMapper.deleteTeamMemberForApplication(ProgramNumber);
    }

    public List<ProgramApplication> getMyProgramApplications(String Username){
        return programMapper.getMyProgramApplications(Username);
    }

    public int passFirstReview(String number){
        int x = programMapper.passFirstReview(number);
        System.out.println("通过一审项目表："+x);
        int y = programMapper.passFirstReviewApplication(number);
        System.out.println("通过一审项目申报表："+y);
        int result = x + y;
        return result;
    }

    public int denyFirstReview(String number){
        int x = programMapper.denyFirstReview(number);
        System.out.println("不通过一审项目表："+x);
        int y = programMapper.denyFirstReviewApplication(number);
        System.out.println("不通过一审项目申报表："+y);
        int result = x + y;
        return result;
    }

    public int passSecondReview(String Number ,String Level){
        int x = programMapper.passSecondReview(Number,Level);
        System.out.println("通过二审项目表："+x);
        int y = programMapper.passSecondReviewApplication(Number,Level);
        System.out.println("通过二审项目申报表："+y);
        int result = x + y;
        return result;
    }

    public int denySecondReview(String Number ){
        int x = programMapper.denySecondReview(Number);
        System.out.println("不通过二审项目表："+x);
        int y = programMapper.denySecondReviewApplication(Number);
        System.out.println("不通过二审项目申报表："+y);
        int result = x + y;
        return result;
    }

    public List<Program> FuzzySearchMyPrograms(String Username,String query){
        query = "%" + query + "%";
        List<Program> Programs = programMapper.FuzzySearch(query);
        List<Program> result = new ArrayList<>();
        for(Program iterator : Programs){
            if(iterator.getManager().equals(Username)){
                    result.add(iterator);
            }
        }
        return result;
    }
}
